package com.itheima.tanhua.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.tanhua.api.mongo.CommentApi;
import com.itheima.tanhuan.CommentType;
import com.itheima.tanhuan.pojo.mongo.Comment;
import com.itheima.tanhuan.pojo.mongo.Movement;
import com.mongodb.client.result.DeleteResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Comment> findByMovementId(ObjectId objectId, CommentType commentType) {
        //当动态id和评论类型id作为条件查询
        Query query = Query.query(Criteria.where("publishId").is(objectId)
                .and("commentType").is(commentType.getType())).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        System.out.println(comments);
        return comments;
    }


    @Override
    public Boolean commentPost(Comment comment1) {
        mongoTemplate.save(comment1, "comment");
        return true;
    }

    //修改评论数并返回like数
    @Override
    public Integer likeCommentCount(String id) {

        Comment findById = findById(id);
        int i = findById.getLikeCount() + 1;
        Update update = new Update();
        update.set("likeCount", i);
        mongoTemplate.updateFirst(Query.query(Criteria.where("id").is(new ObjectId(id))), update, Comment.class);
        return i;
    }

    @Override
    public Integer dislikeCommentCount(String id) {
        Comment findById = findById(id);
        int i = findById.getLikeCount() - 1;
        Update update = new Update();
        update.set("likeCount", i);
        mongoTemplate.updateFirst(Query.query(Criteria.where("id").is(new ObjectId(id))), update, Comment.class);
        return i;
    }

    public Comment findById(String id) {
        Comment findById = mongoTemplate.findById(new ObjectId(id), Comment.class);
        return findById;
    }

    /**
     * @param uid 登录者id
     * @param
     * @param
     * @return
     */
    @Override
    public Boolean deletLike(String uid, String publishId, int commentType) {
        //删除记录的数据
        Query query = Query.query(Criteria.where("publishId").is(publishId)
                .and("userId").is(uid)
                .and("commentType").is(commentType));
        DeleteResult remove = mongoTemplate.remove(query, Comment.class);
        //取消点赞，动态点赞-1
        return ObjectUtil.isEmpty(remove) ? false : true;
    }

    //动态喜欢
    @Override
    public Integer likeComment(String movementId) {


        return null;
    }

    //点过赞了。
    @Override
    public Boolean isLike(String publishId, int commentType, Long uid) {
        Criteria criteria = Criteria.where("userId").is(uid)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(commentType);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, Comment.class); //判断数据是否存在
    }

    @Override
    public Integer save(Comment comment1) {
        Comment comment = mongoTemplate.save(comment1, "comment");
        //动态数量加喜欢+1；
        ObjectId publishId = comment.getPublishId();
        Movement byId = mongoTemplate.findById(publishId, Movement.class);

        //返回点赞数量
        return byId.getLikeCount();
    }

    @Override
    public List<Comment> findByUserId(Long uid, CommentType comment, Long page, Long pagesize) {
        Query query = Query.query(Criteria.where("userId").is(uid)
                .and("commentType").is(comment.getType()))
                .skip(Convert.toLong(page - 1L) * pagesize)
                .limit(Convert.toInt(pagesize));
        return mongoTemplate.find(query, Comment.class);
    }
}